# -*- coding=utf8 -*-

__author__ = 'nickleo'
import string
import jsonpickle as json
from config import HogWartsConfig as cfg
import http_client as http


def get_group():
    result = []
    action_alert = None
    for name_server in cfg().get_nameservers():
        print 'name_server' + name_server
        http_topiclist_result = http.get_topiclist(name_server)
        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            action_alert = 'nameserver:' + name_server + 'is No TopicRoute info No  ConsumerGroup '
        else:
            for _topiclist in json.decode(http_topiclist_result):
                # _topiclist ={"ClusterName":"FetionMQCluster","Topic":"FetionPerformace","ConsumerGroup":"FetionPerformance"}
                if _topiclist['ConsumerGroup'] is None or _topiclist['ConsumerGroup'].strip() == '':
                    x = 1
                else:
                    result.append({'name': _topiclist['ConsumerGroup'], 'value': _topiclist['ConsumerGroup']})

    return result, action_alert


def get_consumer_name():
    result, action_alert = get_group()
    result.append({'name': 'all', 'value': 'all'})
    return result, action_alert


def get_progress_data(name_addr, group):
    action_alert = ""
    result = []
    if name_addr is None or name_addr.strip() == '':
        name_addr = cfg().get_nameservers()[0]
        print name_addr
    if group is None or group.strip() == '':
        group = 'all'
    result = get_progress_data_inner(name_addr, group)
    return result, action_alert


def get_progress_data_inner(name_addr, group):
    result = []
    print group
    if group == 'all':
        groups, _ = get_consumer_name()
        for tmpg in groups:
            if tmpg['name'] == 'all':
                continue
            else:
                result += __get_progress_data_one__(name_addr, tmpg['name'])
    else:
        result += __get_progress_data_one__(name_addr, group)

    return result


def __get_color__(nomstr):
    num = string.atoi(nomstr)
    if num < 800:
        return 'success'
    elif num < 1200:
        return 'warning'
    else:
        return 'danger'


def __get_progress_data_one__(name_addr, group):
    result = []
    # [{"ConsumerOffset":"0","Diff":"0","QUID":"0","Topic":"%RETRY%fetion5","BrokerOffset":"0","BrokerName":"fetionmq"},{"ConsumerOffset":"6","Diff":"0","QUID":"0","Topic":"Fetion","BrokerOffset":"6","BrokerName":"fetionmq"},{"ConsumerOffset":"6","Diff":"0","QUID":"1","Topic":"Fetion","BrokerOffset":"6","BrokerName":"fetionmq"},{"ConsumerOffset":"4","Diff":"0","QUID":"2","Topic":"Fetion","BrokerOffset":"4","BrokerName":"fetionmq"},{"ConsumerOffset":"4","Diff":"0","QUID":"3","Topic":"Fetion","BrokerOffset":"4","BrokerName":"fetionmq"},{"ConsumeTPS":"0","DiffTotal":"0"}]
    http_result = http.get_progress_data_one(name_addr, group)
    print http_result
    if http_result is None or http_result.strip() == '':
        action_alert = 'nameserver:' + name_addr + 'is No Clusters '
    else:
        for line in json.decode(http_result):
            result.append(
                {'Color': __get_color__(line['Diff']), 'GroupName': group, "ConsumerOffset": line['ConsumerOffset'],
                 "Diff": line['Diff'], "QUID": line['QUID'], "Topic": line['Topic'],
                 "BrokerOffset": line['BrokerOffset'], "BrokerName": line['BrokerName']})

    return result
